package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xiaojian.pick.entity.Topic;
import com.xiaojian.pick.entity.TopicImg;
import com.xiaojian.pick.entity.User;
import com.xiaojian.pick.mapper.TopicMapper;
import com.xiaojian.pick.page.TopicCustom;
import com.xiaojian.pick.service.TopicImgService;
import com.xiaojian.pick.service.TopicService;
import com.xiaojian.pick.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小贱
 * @date 2020/11/22 - 22:02
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    private TopicMapper topicMapper;

    @Resource
    private TopicImgService topicImgService;

    @Resource
    @Lazy
    private UserService userService;

    @Override
    public Topic findById(Integer id) {
        Topic topic = topicMapper.selectById(id);
        User user = userService.findById(topic.getUserId());
        topic.setUser(user);
        int count = count(new QueryWrapper<Topic>().eq("user_id", topic.getUserId()));
        topic.setTotal(count);
        return topic;
    }

    @Override
    public List<Topic> findByUserId(Integer userId) {
        return topicMapper.findByUserId(userId);
    }

    @Override
    public List<Topic> findAll() {
        return topicMapper.findAll();
    }

    @Override
    public List<Topic> findByParam(TopicCustom topicCustom,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        return topicMapper.findByParam(topicCustom);
    }

    @Override
    public Long getCount(TopicCustom topicCustom) {
        return topicMapper.getCount(topicCustom);
    }

    @Transactional
    @Override
    public int addTopic(Topic topic) {
        boolean save = this.save(topic);
        List<TopicImg> topicImgList = topic.getTopicImgList();
        if(topicImgList != null && topicImgList.size() > 0){
            topicImgList.forEach(image -> {
                image.setTopicId(topic.getId());
            });
            topicImgService.saveBatch(topicImgList);
        }
        return topic.getId();
    }

    @Override
    public int updateTopicState(Topic topic) {
        return topicMapper.updateTopicState(topic);
    }

    @Override
    public int updateTopicHot(Topic topic) {
        return topicMapper.updateTopicHot(topic);
    }

    @Override
    public int addTopicClick(Integer id) {
        return topicMapper.addTopicClick(id);
    }

    @Override
    public int addTopicComment(Integer id) {
        return topicMapper.addTopicComment(id);
    }

    @Override
    public int subTopicComment(Integer id) {
        return topicMapper.subTopicComment(id);
    }

    @Override
    public List<Topic> getUserPublish(Topic topic) {
        return topicMapper.getUserPublish(topic);
    }

    @Override
    public List<Topic> findByPage(Topic topic, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        return topicMapper.findByPage(topic);
    }

    @Override
    public List<Topic> search(String content, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        return topicMapper.search(content);
    }

    @Override
    public List<Topic> overtimeTopic() {
        return topicMapper.overtimeTopic();
    }

    @Override
    public int realDelTopic(Integer id) {
        return topicMapper.realDelTopic(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void modifySupportCount(Integer topicId, int count) {
        this.update(new UpdateWrapper<Topic>().setSql("support_count = support_count + " + count)
                .eq("id", topicId).gt(count < 0, "support_count", 0));
    }
}
